package com.and9.mchen.mcmusic.dao.impl;

import java.sql.Timestamp;
import java.text.MessageFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.and9.mchen.mcmusic.dao.IBaseEntityDao;
import com.and9.mchen.mcmusic.dao.IIntroductionDao;
import com.and9.mchen.mcmusic.entity.Introduction;
import com.and9.mchen.mcmusic.exception.DBException;
import com.and9.mchen.mcmusic.exception.DaoException;
import com.and9.mchen.mcmusic.util.EntityHelper;
import com.and9.mchen.mcmusic.util.GEntityHelper;
import com.and9.mchen.mcmusic.util.QueryHelper;

public class IntroductionDaoImpl implements IIntroductionDao{

	private final static Log log = LogFactory.getLog(IntroductionDaoImpl.class);
	
	public static final String TABLEMANE="introduction_table";
	
	public int createOrUpdateIntroduction(Introduction introduction,
			String... propertyNames) throws DaoException {

		if(GEntityHelper.isEmpty(introduction)){
			return -1;
		}
		
		String sqlStr=null;
		Object[] propertyValues=null;
		
		if(introduction.getId() == null || introduction.getId() <= 0){
			
			if(GEntityHelper.isEmpty(introduction.getUniqueId())){
				introduction.setUniqueId(EntityHelper.createUniqueId());
			}
			introduction.setCreateTime(new Timestamp(System.currentTimeMillis()));
			Object[] parentParams=QueryHelper.getFieldsVal(introduction.getParent(), IBaseEntityDao.BASEPROPERTYNAMES);
			Object[] introductionParams=QueryHelper.getFieldsVal(introduction, IIntroductionDao.BASEPROPERTYNAMES);
			propertyValues=EntityHelper.groupArrays(parentParams, introductionParams);

			propertyNames=EntityHelper.groupArrays(IBaseEntityDao.BASEPROPERTYNAMES, IIntroductionDao.BASEPROPERTYNAMES);
			sqlStr=QueryHelper.getInsertSQLByProperty(IntroductionDaoImpl.TABLEMANE, propertyNames);
			
		}else{
			
			if(introduction.getId().equals(1)){
				return 0;
			}
			
			if(GEntityHelper.isEmpty(propertyNames) || 
					EntityHelper.containBaseEntityPropertyName(propertyNames) == true){
				propertyNames=IntroductionDaoImpl.BASEPROPERTYNAMES;
			}
			propertyValues=QueryHelper.getFieldsVal(introduction, propertyNames);
			sqlStr=QueryHelper.getUpdateSQL(IntroductionDaoImpl.TABLEMANE, introduction,"id="+introduction.getId(), propertyNames);
		}
		
		try {
			
			return QueryHelper.update(sqlStr, propertyValues);
		} catch (DBException e) {
			log.error("[function: createOrUpdateintroduction]", e);
			throw new DaoException("create or update introduction faliure");
		}
	}

	public Introduction getIntroductionByIntroductionId(long id) throws DaoException {
	
		if(id <= 0){
			return null;
		}
		
		String sqlStr=QueryHelper.getSelectSQL(IntroductionDaoImpl.TABLEMANE, "id=?");
		
		try {
			return QueryHelper.read(Introduction.class, sqlStr, id);
		} catch (DBException e) {
			log.error("[function: getintroductionByintroductionId]", e);
			throw new DaoException("get introduction by introduction id faliure");
		}
	}

	public Introduction getIntroductionByIntroductionUniqueId(String uniqueId)
			throws DaoException {
		
		if(GEntityHelper.isEmpty(uniqueId)){
			return null;
		}
		
		String sqlStr=QueryHelper.getSelectSQL(IntroductionDaoImpl.TABLEMANE, "uniqueId=?");

		try {
			return QueryHelper.read(Introduction.class, sqlStr, uniqueId);
		} catch (DBException e) {
			log.error("[function: getintroductionByintroductionUniqueId]", e);
			throw new DaoException("get introduction by introduction unique id faliure");
		}
	}

	public int deleteIntroductionByIntroductionId(long id) throws DaoException {

		if(id <= 0){
			return -1;
		}
		
		if(id == 1){
			return 0;
		}
		
		String sqlStr=QueryHelper.getDeleteSQL(IntroductionDaoImpl.TABLEMANE, "id=?");
		
		try {
			return QueryHelper.update(sqlStr, id);
		} catch (DBException e) {
			log.error("[function: deleteintroductionByintroductionId]", e);
			throw new DaoException("delete introduction by introduction id faliure");
		}
		
	}

	public Introduction getIntroductionBySingerId(long id) throws DaoException {
		
		if(id <= 0){
			return null;
		}
		
		String sqlStr=MessageFormat.format("SELECT DISTINCT {0}.* FROM {0}, {1} WHERE " +
				"{0}.id={1}.introduction_id AND {1}.id=? LIMIT 1", IntroductionDaoImpl.TABLEMANE, 
					SingerDaoImpl.TABLEMANE);
		
		try {
			return QueryHelper.read(Introduction.class, sqlStr, id);
		} catch (DBException e) {
			log.error("[function: getIntroductionBySingerId]", e);
			throw new DaoException("get introduction by album id faliure");
		}
		
	}

	public Introduction getIntroductionByAlbumId(long id) throws DaoException {
		
		if(id <= 0){
			return null;
		}
		
		String sqlStr=MessageFormat.format("SELECT DISTINCT {0}.* FROM {0}, {1} WHERE " +
				"{0}.id={1}.introduction_id AND {1}.id=? LIMIT 1", IntroductionDaoImpl.TABLEMANE, 
					AlbumDaoImpl.TABLEMANE);
		
		try {
			return QueryHelper.read(Introduction.class, sqlStr, id);
		} catch (DBException e) {
			log.error("[function: getIntroductionByAlbumId]", e);
			throw new DaoException("get introduction by album id faliure");
		}
	}

}
